import sys
sys.path.append("../")

from controller.BaseConfroller import BaseController
from frameworks.utils.MouseUtil import *
from frameworks.utils.KeyBoardUtil import *
from selenium.webdriver.common.by import By
import time
import os
import json

class CrawGaoKaoController(BaseController):
    def __init__(self):
        super(CrawGaoKaoController, self).__init__()

    def run(self):
        driver = self.driver
        for i in range(1,15):
            input = driver.find_element(By.XPATH, '//*[@class="ant-pagination-options-quick-jumper"]/input')
            input.clear()
            input.send_keys(i)
            KeyBoardUtil.oneHot("enter")
            time.sleep(5)
            data = self.getPageData()
            with open("./data"+str(i)+".csv","w+") as file:
                file.write(json.dumps(data))
            time.sleep(5)

    def contains_chinese(self,text):
        for char in text:
            if 19968 <= ord(char) <= 40869:
                return True
        return False

    def getPageData(self):
        driver = self.driver
        divs = driver.find_element(By.XPATH, '//*[@class="min-h-400px"]')
        user_elements = divs.find_elements(By.XPATH, "./div")
        allinfodata = []
        for row in user_elements:
            print(row.text)
            rowinfo = row.text.split("\n")
            allinfo = {}
            if len(rowinfo[1]) < 4 or self.contains_chinese(rowinfo[1]) == False:
                continue
            allinfo["school_name"] = rowinfo[1]
            allinfo["province"] = rowinfo[2]
            allinfo["class"] = rowinfo[3]
            allinfo["type"] = rowinfo[4]
            print("=========================================")
            print(allinfo)
            option = row.find_element(By.XPATH, "./div[3]/button")
            driver.execute_script("arguments[0].click();", option)
            time.sleep(10)
            detail = driver.find_element(By.CLASS_NAME, 'ant-modal-root')
            options1 = detail.find_element(By.CLASS_NAME, 'ant-spin-nested-loading')
            content1 = options1.find_elements(By.XPATH, "./div/div/div")
            allinfo["data"] = []
            if len(content1) < 1:
                continue
            for option1 in content1:
                special = {}
                print("==================option=====================")
                allcontent = option1.text.split("\n")
                special["class_name"] = allcontent[2]
                special["person"] = allcontent[4]
                special["money"] = allcontent[6]
                special["data"] = []
                twooptions = option1.find_elements(By.XPATH, "./div[2]")
                for third in twooptions:
                    print("==================third=======================")
                    four = third.find_elements(By.XPATH, "./div[3]/div")
                    specialdata = []
                    if len(four) < 1:
                        continue
                    for fourOption in four:
                        print("========================four==============")
                        print(fourOption.text)
                        alldetail = fourOption.text.split("\n")
                        if len(alldetail) < 3:
                            continue
                        specialdataOption = {}
                        specialdataOption["year"] = alldetail[0]
                        specialdataOption["person"] = alldetail[1]
                        specialdataOption["low"] = alldetail[2]
                        specialdataOption["align"] = alldetail[3]
                        special["data"].append(specialdataOption)
                allinfo["data"].append(special)
            allinfodata.append(allinfo)
            close = driver.find_element(By.CLASS_NAME, "ant-modal-close")
            driver.execute_script("arguments[0].click();", close)
            time.sleep(5)
        return allinfodata
